package com.example.gametouch;

import java.util.Random;

import android.app.Activity;
import android.app.PendingIntent.OnFinished;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class NewGame extends Activity {
	CountDownTimer count_obj;
	ImageView img1, img2, img3, img4, img5, img6, img7, img8, img9, img10,
			img11, img12, img13, img14, img15, img16, img17, img18, img19,
			img20, img21, img22, img23, img24;
	int x, DoKhoDe;
	int xtam_old;
	int biendem;
	boolean flag, flag_end;
	TextView tv, diem, tv2, Name;
	int n = 0;
	int time;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.newgame);
		Name = (TextView) findViewById(R.id.Name);
		tv = (TextView) findViewById(R.id.tv);
		/* tv2 = (TextView) findViewById(R.id.tv2); */
		diem = (TextView) findViewById(R.id.diem);

		img1 = (ImageView) findViewById(R.id.imageView1);
		img2 = (ImageView) findViewById(R.id.imageView2);
		img3 = (ImageView) findViewById(R.id.imageView3);
		img4 = (ImageView) findViewById(R.id.imageView4);

		img5 = (ImageView) findViewById(R.id.imageView5);
		img6 = (ImageView) findViewById(R.id.imageView6);
		img7 = (ImageView) findViewById(R.id.imageView7);
		img8 = (ImageView) findViewById(R.id.imageView8);

		img9 = (ImageView) findViewById(R.id.imageView9);
		img10 = (ImageView) findViewById(R.id.imageView10);
		img11 = (ImageView) findViewById(R.id.imageView11);
		img12 = (ImageView) findViewById(R.id.imageView12);

		img13 = (ImageView) findViewById(R.id.imageView13);
		img14 = (ImageView) findViewById(R.id.imageView14);
		img15 = (ImageView) findViewById(R.id.imageView15);
		img16 = (ImageView) findViewById(R.id.imageView16);

		img17 = (ImageView) findViewById(R.id.imageView17);
		img18 = (ImageView) findViewById(R.id.imageView18);
		img19 = (ImageView) findViewById(R.id.imageView19);
		img20 = (ImageView) findViewById(R.id.imageView20);

		img21 = (ImageView) findViewById(R.id.imageView21);
		img22 = (ImageView) findViewById(R.id.imageView22);
		img23 = (ImageView) findViewById(R.id.imageView23);
		img24 = (ImageView) findViewById(R.id.imageView24);

		final ImageView mang[] = { img1, img2, img3, img4, img5, img6, img7,
				img8, img9, img10, img11, img12, img13, img14, img15, img16,
				img17, img18, img19, img20, img21, img22, img23, img24 };
		final int mang_hinh[] = { R.drawable.icon_03, R.drawable.icon_17,
				R.drawable.icon_06, R.drawable.icon_12, R.drawable.icon_26,
				R.drawable.icon_42, R.drawable.icon_09, R.drawable.icon_24,
				R.drawable.icon_38, R.drawable.icon_40, R.drawable.icon_115,
				R.drawable.icon_118, R.drawable.icon_114, R.drawable.icon_116,
				R.drawable.icon_119, R.drawable.icon_121, R.drawable.icon_123,
				R.drawable.icon_122, R.drawable.icon_120, R.drawable.icon_124,
				R.drawable.icon_127, R.drawable.icon_129, R.drawable.icon_125,
				R.drawable.icon_126 };

		flag = false;
		flag_end = true;

		Intent intent = getIntent();
		Bundle lay = intent.getExtras();
		// DoKhoDe = lay.getInt("DOKHO");
		boolean co = lay.getBoolean("CO");
		if (co == true) {
			DoKhoDe = lay.getInt("LV");

			x = lay.getInt("VT");
			n = lay.getInt("DS");

			String text = lay.getString("NAME");
			Name.setText(text);
			time = lay.getInt("TIME");
			biendem = time;
			Log.d("LAY", "" + x + " " + n + " " + DoKhoDe + " " + biendem);
			diem.setText("" + n);
		} else {
			biendem = lay.getInt("BIENDEM");
			DoKhoDe = lay.getInt("DOKHO");
			String text = lay.getString("NAME");
			Name.setText(text);
			time = biendem;

		}

		

		new CountDownTimer(biendem, DoKhoDe) {

			@Override
			public void onTick(long millisUntilFinished) {
				// TODO Auto-generated method stub
				if (flag_end == true) {
					if (flag == true) {
						mang[xtam_old].setImageResource(mang_hinh[xtam_old]);
						mang[xtam_old]
								.setOnClickListener(new View.OnClickListener() {

									@Override
									public void onClick(View arg0) {
										// TODO Auto-generated method stub

									}
								});
					}
					
						time =time - 1000;
					
					Random ran = new Random();
					x = ran.nextInt(24);

					mang[x].setOnClickListener(new View.OnClickListener() {

						@Override
						public void onClick(View arg0) {
							// TODO Auto-generated method stub

							n += 1;
							diem.setText("" + n);

						}

					});

					mang[x].setImageResource(R.drawable.icon_25);
					xtam_old = x;
					flag = true;

					tv.setText("  " + formatTime(millisUntilFinished));
				}

			}

			@Override
			public void onFinish() {
				// TODO Auto-generated method stub
				try {
					createShareP();
				} catch (Exception e) {
					// TODO Auto-generated catch block
					Toast.makeText(getApplicationContext(), "không ghi được",
							Toast.LENGTH_SHORT).show();
				}

				if (flag_end == true) {
					for (int i = 0; i < mang.length; i++) {
						mang[i].setImageResource(R.drawable.icon111);
					}
					if (n < 15) {
						tv.setText("Bạn đã thất bại");

					} else
						tv.setText("Chiến Thắng");
				}

			}
		}.start();

	}

	private void createShareP() {
		// b1 mo file

		SharedPreferences SP = getSharedPreferences("SAVE",
				Activity.MODE_PRIVATE);
		//
		SharedPreferences.Editor editor = SP.edit();
		editor.putInt("VITRI", x);
		editor.putInt("LV", DoKhoDe);
		editor.putInt("DIEM", n);
		editor.putInt("TIME", time);
		editor.putString("NAME", Name.getText().toString());
		Log.d("LUU", "Diem:" + n + " " + "ViTri:" + x + " " + "Dokho: "
				+ DoKhoDe + " " + "Time:" + biendem + " " + "Ten: "
				+ Name.getText().toString() + " " + "Thoi gian:" + time);

		// dung commit de update noi dung chung ta vua moi tao ra
		editor.commit();
	}

	public String formatTime(long millis) {
		String output = "00:00";
		long seconds = millis / 1000;
		long minutes = seconds / 60;

		seconds = seconds % 60;
		minutes = minutes % 60;

		String sec = String.valueOf(seconds);
		String min = String.valueOf(minutes);

		if (seconds < 10)
			sec = "0" + seconds;
		if (minutes < 10)
			min = "0" + minutes;

		output = min + " : " + sec;

		return output;
	}// formatTime

	@Override
	protected void onPause() {
		// khi dung ung dung
		createShareP();
		Intent back = new Intent(this, Game_Touch.class);
		startActivity(back);

		super.onPause();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.game__touch, menu);
		return true;
	}

}
